//
//  ViewController.m
//  testdemo
//
//  Created by 王洪飞 on 2024/6/9.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) dispatch_queue_t queueA;
@property (nonatomic, strong) dispatch_queue_t queueB;
@end

@implementation ViewController

static void *openGLESContextQueueKey;
static void *openGLESContextQueueKey1;

static int const kQueueAKey = 0;
static int const kQueueBKey = 1;

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        NSLog(@"init nib");
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        NSLog(@"init coder");
        openGLESContextQueueKey = &openGLESContextQueueKey;
        openGLESContextQueueKey1 = &openGLESContextQueueKey1;
    }
    return self;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        NSLog(@"init");
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor cyanColor];
    dispatch_queue_t queueA = dispatch_queue_create("AA", NULL);
    dispatch_queue_t queueB = dispatch_queue_create("BB", NULL);
    self.queueA = queueA;
    self.queueB = queueB;
    dispatch_queue_set_specific(queueA, &kQueueAKey, (__bridge void *)self, NULL);
    dispatch_queue_set_specific(queueB, &kQueueBKey, (__bridge void *)self, NULL);

//    dispatch_set_target_queue(queueB, queueA);


//    [self taskAction];
    dispatch_sync(_queueB, ^{
        [self taskAction];
    });
}



-(void)taskAction {
    dispatch_sync(self.queueA, ^{
        // 任务1
        NSLog(@"任务1");
        dispatch_block_t task2 = ^{
            NSLog(@"任务2");
        };
        BOOL result = dispatch_get_specific(&kQueueBKey) == &kQueueBKey;
//        if (dispatch_get_current_queue() == self.queueB) {
        if(result){
            task2();
        } else {
//            dispatch_sync(self.queueB, task2);
//            task2();
           
        }
        
        // 任务3必须在任务2完成后才可以继续
        NSLog(@"任务3");
    });
}





@end
